package models

import (
	"errors"
	"gitee.com/ha666/golibs"
	"strconv"
)

type Ipblacklist struct {
	IP       string //IP地址
	Status   int    //状态
	ExpireIn int64  //过期时间
}

func ExistIpblacklist(IP string) (bool, error) {
	rows, err := Ha666DBConn.Query("select count(0) Count from ipblacklist where IP=?", IP)
	if err != nil {
		return false, err
	}
	if len(rows) <= 0 {
		return false, nil
	}
	for _, obj := range rows {
		count, err := strconv.Atoi(string(obj["Count"]))
		if err != nil {
			return false, errors.New("parse Count error: " + err.Error())
		}
		return count > 0, nil
	}
	return false, nil
}

func InsertIpblacklist(ipblacklist Ipblacklist) (bool, error) {
	result, err := Ha666DBConn.Exec("insert into ipblacklist(IP,Status,ExpireIn) values(?,?,?)", ipblacklist.IP, ipblacklist.Status, ipblacklist.ExpireIn)
	if err != nil {
		return false, err
	}
	affected, err := result.RowsAffected()
	if err != nil {
		return false, err
	}
	return affected > 0, nil
}

func UpdateIpblacklist(ipblacklist Ipblacklist) (bool, error) {
	result, err := Ha666DBConn.Exec("update ipblacklist set Status=?, ExpireIn=? where IP=?", ipblacklist.Status, ipblacklist.ExpireIn, ipblacklist.IP)
	if err != nil {
		return false, err
	}
	affected, err := result.RowsAffected()
	if err != nil {
		return false, err
	}
	return affected > 0, nil
}

func InsertUpdateIpblacklist(ipblacklist Ipblacklist) (bool, error) {
	result, err := Ha666DBConn.Exec("insert into ipblacklist(IP,Status,ExpireIn) values(?,?,?) ON DUPLICATE KEY UPDATE Status=?,ExpireIn=?", ipblacklist.IP, ipblacklist.Status, ipblacklist.ExpireIn, ipblacklist.Status, ipblacklist.ExpireIn)
	if err != nil {
		return false, err
	}
	affected, err := result.RowsAffected()
	if err != nil {
		return false, err
	}
	return affected > 0, nil
}

func GetIpblacklist(IP string) (ipblacklist Ipblacklist, err error) {
	rows, err := Ha666DBConn.Query("select IP, Status, ExpireIn from ipblacklist where IP=?", IP)
	if err != nil {
		return ipblacklist, err
	}
	if len(rows) <= 0 {
		return ipblacklist, nil
	}
	ipblacklists, err := _IpblacklistRowsToArray(rows)
	if err != nil {
		return ipblacklist, err
	}
	return ipblacklists[0], nil
}

// idx_ipblacklist_status
func GetIpblacklistByStatus(Status int) (ipblacklists []Ipblacklist, err error) {
	rows, err := Ha666DBConn.Query("select IP, Status, ExpireIn from ipblacklist where status=?", Status)
	if err != nil {
		return ipblacklists, err
	}
	if len(rows) <= 0 {
		return ipblacklists, nil
	}
	return _IpblacklistRowsToArray(rows)
}

func GetIpblacklistRowCount() (count int, err error) {
	rows, err := Ha666DBConn.Query("select count(0) Count from ipblacklist")
	if err != nil {
		return -1, err
	}
	if len(rows) <= 0 {
		return -1, nil
	}
	for _, obj := range rows {
		count, err := strconv.Atoi(string(obj["Count"]))
		if err != nil {
			return -1, errors.New("parse Count error: " + err.Error())
		}
		return count, nil
	}
	return -1, nil
}

// idx_ipblacklist_status
func GetIpblacklistRowListByStatus(StatusIsDesc bool, PageIndex, PageSize int) (ipblacklists []Ipblacklist, err error) {
	sqlstr := golibs.NewStringBuilder()
	sqlstr.Append("select IP, Status, ExpireIn from ipblacklist order by")
	sqlstr.Append(" Status")
	if StatusIsDesc {
		sqlstr.Append(" Desc")
	}
	sqlstr.Append(" limit ?,?")
	rows, err := Ha666DBConn.Query(sqlstr.ToString(), (PageIndex-1)*PageSize, PageSize)
	sqlstr.Append(",")
	if err != nil {
		return ipblacklists, err
	}
	if len(rows) <= 0 {
		return ipblacklists, nil
	}
	return _IpblacklistRowsToArray(rows)
}

func _IpblacklistRowsToArray(maps []map[string][]byte) ([]Ipblacklist, error) {
	models := make([]Ipblacklist, len(maps))
	var err error
	for index, obj := range maps {
		model := Ipblacklist{}
		model.IP = string(obj["IP"])
		model.Status, err = strconv.Atoi(string(obj["Status"]))
		if err != nil {
			return nil, errors.New("parse Status error: " + err.Error())
		}
		model.ExpireIn, err = strconv.ParseInt(string(obj["ExpireIn"]), 10, 64)
		if err != nil {
			return nil, errors.New("parse ExpireIn error: " + err.Error())
		}
		models[index] = model
	}
	return models, err
}
